var session = sessionStorage.getItem("session");
var Search, sDate, eDate, device, areatext, Confirm;
if (!session) {
	window.parent.location = "../../login.html";
}
$(function() {
	$('#area').combobox({
		url: IP + "GetDepartmentInfo?callback=?",
		valueField: 'id',
		textField: 'text',
		editable: false,
	});
	$('#sDate').datetimebox('setValue', ThreeDayBefore);
	$("input", $("#DeviceId").next("span")).click(function() {
		$('#DeviceIdTab').window('open');
	});
	$("#QueryOffLineWaingInfo").jqGrid({
		colModel: [{
			label: '设备编码',
			name: 'deviceid'
		}, {
			label: '操作人',
			name: 'operate'
		}, {
			label: '告警名称',
			name: 'wName'
		}, {
			label: '告警类型',
			name: 'wType'
		}, {
			label: '告警条件',
			name: 'wCondition'
		}, {
			label: '离线时间',
			name: 'offTime'
		}, {
			label: '创建时间',
			name: 'cTime'
		}, {
			label: '告警状态',
			name: 'wState'
		}],
		pagerpos: 'center',
		viewrecords: true,
		height: 480,
		rowNum: 15,
		autowidth: true,
		loadonce: true,
		page: 1,
		subGrid: true, // set the subGrid property to true to show expand buttons for each row
		//		subGridRowExpanded: showChildGrid, // javascript function that will take care of showing the child grid
		loadtext: '正在加载数据',
		pager: "#QueryOffLineWaingInfoGrid",
		sortorder: "asc",
		sortname: 'id'
	});

	$(window).bind('resize', function() {
		$("#QueryOffLineWaingInfo").setGridWidth($(window).width() * 1);
		$("#QueryOffLineWaingInfo").setGridHeight($(window).height() - 110);
	});
	ExportFile = function() {
		$('#QueryOffLineWaingInfo').jqGrid('setGridParam', {
			rowNum: 1000,
		}).trigger('reloadGrid');
		var title = "Ftp上传测试报告";
		getXlsFromTbl('QueryOffLineWaingInfo', 'gridtable', title, true);
		setTimeout(BackGrid(), 500);

		function BackGrid() {
			$('#QueryOffLineWaingInfo').jqGrid('setGridParam', {
				rowNum: 17,
			}).trigger('reloadGrid');
		}
	}

	$('#QueryOffLineWaingInfo').navGrid("#QueryOffLineWaingInfoGrid", {
		search: false, // show search button on the toolbar
		add: false,
		edit: false,
		del: false,
		refresh: true
	});

	Confirm = function() {
		sDate = $('#sDate').datetimebox('getValue').substr(0, 10);
		eDate = $('#eDate').datetimebox('getValue').substr(0, 10);
		device = $('#DeviceId').textbox('getValue');
		areatext = $('#area').combobox('getText');
		console.log(device + ',' + areatext);
		if (!DateRight(sDate, eDate)) {
			$.messager.show({
				title: '提醒',
				msg: '请选择正确的时间范围！',
				timeout: 1300,
				showType: 'slide',
				style: {
					right: '',
					bottom: ''
				}
			});
		} else if (!device) {

			$.messager.show({
				title: '提示！',
				msg: '请选择设备信息',
				showType: 'fade',
				style: {
					right: '',
					bottom: ''
				}
			})
		} else if (!areatext) {
			$.messager.show({
				title: '提示！',
				msg: '请选择县市信息',
				showType: 'fade',
				style: {
					right: '',
					bottom: ''
				}
			})
		} else {
			var myurl = IP + "QueryOffLineWaingInfo?callback=?&session=" + session + "&city=" + areatext + "&vKey=" + device + "&sDate=" + sDate + "&eDate=" + eDate;
			$('#QueryOffLineWaingInfo').jqGrid('setGridParam', {
				url: myurl,
				type: 'Get',
				datatype: "jsonp",
			}).trigger('reloadGrid');
			console.log(myurl);
			insertopertlog('告警管理', '通断告警', '查询', '', '');
		}
	}



	jQuery("#DeviceGrid").jqGrid({
		url: IP + "GetProbeInfoList?callback=?&session=" + session,
		datatype: "jsonp",
		height: 175,
		width: 383,
		colNames: ['设备名称', '设备唯一编码'],
		colModel: [{
			name: 'deviceName',
			index: 'deviceName',
			sopt: ['eq'],
			width: 20
		}, {
			name: 'deviceid',
			index: 'deviceid',
			sopt: ['eq'],
			width: 40
		}],
		autowidth: false,
		shrinkToFit: true,
		//		shrinkToFit:false,
		loadonce: true,
		mtype: "GET",
		//		multiselect: true,
		multiselectWidth: 80,
		page: 1,
		viewrecords: true,
		ondblClickRow: function() {
			Search();
		}
	});
	Search = function() {
		var id = $("#DeviceGrid").jqGrid('getGridParam', 'selrow'); //获取选中行号
		var deviceid = $("#DeviceGrid").getCell(id, 'deviceid');
		$("#DeviceId").textbox('setValue', deviceid);
		$('#DeviceIdTab').window('close');
	}
	var timer;
	$("#SearchCell").on("keyup", function() {
		var self = this;
		if (timer) {
			clearTimeout(timer);
		}
		timer = setTimeout(function() {
			//timer = null;
			$("#DeviceGrid").jqGrid('filterInput', self.value);
		}, 0);
	});



})